package com.tuanzi.loan.business.service.statistics.impl;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.statistics.AdverChannelTypeUser;
import com.tuanzi.loan.business.repository.statistics.AdverChannelTypeUserRepository;
import com.tuanzi.loan.business.service.statistics.AdverChannelTypeUserService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.Collaborate.CollaborateAdvertiseConfigListRequest;
import com.tuanzi.loan.vo.response.collaborate.CollaborateAdvertiseConfigListResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author Simon
 * @date 2019/07/29 14:29
 */
@Slf4j
@Debug
@Service
@Transactional
@Retryable(value = {RetryException.class})
public class AdverChannelTypeUserServiceimpl implements AdverChannelTypeUserService {

    @Autowired
    private AdverChannelTypeUserRepository adverChannelTypeUserRepository;

    @Override
    public Page<CollaborateAdvertiseConfigListResponse> list(PageSearcher<CollaborateAdvertiseConfigListRequest> pageSearcher) {
        return adverChannelTypeUserRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
    }


    @Override
    public AdverChannelTypeUser findOne(Long id){
        return adverChannelTypeUserRepository.findOne(id);
    }
    @Override
    public List<AdverChannelTypeUser> findAccount(Long id){
        String account = adverChannelTypeUserRepository.findOne(id).getUserName();
        return adverChannelTypeUserRepository.findCurrentUserChannels(account);
    }
    @Override
    public AdverChannelTypeUser saveOrUpdate(AdverChannelTypeUser advertise) {
        return adverChannelTypeUserRepository.save(advertise);
    }

    public List<AdverChannelTypeUser> findCurrentUserChannels() {
        String account = CurrentUser.get().getAccount();
        List<AdverChannelTypeUser> channelUsers = adverChannelTypeUserRepository.findCurrentUserChannels(account);
        if(channelUsers == null || channelUsers.size() <=0){
            return Lists.newArrayList();
        }
        return channelUsers;
    }

    public void delete(Long id) {
        adverChannelTypeUserRepository.delete(id);
    }
    public void delete(String account) {
        adverChannelTypeUserRepository.delete(account);
    }

}
